<?php
/**

 * @date 2018年3月1日
 *  友情链接控制器
 */
namespace app\admin\controller;
use app\admin\model\Links as LinksModel;
class Links extends Common
{
    private $model;
	private $PageCount;
    public function _initialize()
    {
		parent::_initialize();
        $this->model = new LinksModel();
		$this->PageCount=20;
    }
    // 友情链接列表
    public function index()
    {
		$UrlParam=input();
		$id = intval(input('id'));
		$result = $this->model->getLink($id);
        if ($id && $result) {
            $this->assign('link', $result);
        } else {
            $UrlParam['id']=false;
             if (! ! ($field = input('field')) && ! ! ($keyword = trim(input('keyword')))) {
				$UrlParam['field']=$keyword;
                $result = $this->model->findLink($UrlParam,$this->PageCount);
            } else {
                $result = $this->model->getList($UrlParam,$this->PageCount);
            }
            $this->assign('gids', $this->model->getGid());
			$this->assign('UrlParam', $UrlParam);
            $this->assign('links', $result);
        }
        return $this->fetch();
    }

    // 友情链接增加
    public function add()
    {
        if($this->request->isPost()) {
            // 获取数据
            $gid = input('gid/s');
            $name = input('name/s');
            $link = input('link/s');
            $logo = input('logo/s');
            $sorting = input('sorting/d');
            
            if (! $gid) {
                $gid = $this->model->getMaxGid() + 1;
            }
            
            if (! $name) {
                alert('名称不能为空！','back');
            }
            
            if (! $link) {
                alert('链接不能为空！','back');
            }
            
            if (! $sorting) {
                $sorting = 255;
            }            
            // 构建数据
            $data = array(
                'acode' =>'cn',
                'gid' => $gid,
                'name' => $name,
                'link' => $link,
                'logo' => $logo,
                'sorting' => $sorting,
				'create_time' => date('Y-m-d H:i:s',time()),
				'update_time' => date('Y-m-d H:i:s',time()),
                'create_user' => session('username'),
                'update_user' => session('username')
            );
            
            // 执行添加
            if ($this->model->addLink($data)) {
                $this->success('新增成功！', url('/Links/index'));
            } else {
                $this->error('新增失败！', - 1);
            }
        } else {
            return $this->fetch('index');
        }
    }
    // 友情链接删除
    public function del()
    {
        if (! $id = input('id/s')) {
            alert('传递的参数值错误！','back');
        }
        
        if ($this->model->delLink($id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    // 友情链接修改
    public function mod()
    {
        $UrlParam=input();
		$submit = trim(input('submit'));
        if ($submit) {
            switch ($submit) {
                case 'sorting': // 修改列表排序
                    $listall = input('post.listall/a');
                    if ($listall) {
                        $sorting = input('post.sorting/a');
                        foreach ($listall as $key => $value) {
                            if ($sorting[$key] === '' || ! is_numeric($sorting[$key]))
                                $sorting[$key] = 255;
								$strongdata['sorting']=$sorting[$key];
                            $this->model->modLink($value, $strongdata);
                        }
                        $this->success('修改成功！');
                    } else {
                        alert('排序失败，无任何内容！','back');
                    }
                    break;
            }
        }
        
        if (! $id = input('id/s')) {
            alert('传递的参数值错误！','back');
        }
		$this->assign('UrlParam', $UrlParam);        
        // 单独修改状态
        if (($field = input('field')) && ! is_null($value = input('value/d'))) {
			$info[$field]=$value;
			$info['update_user']= session('username');
            if ($this->model->modLink($id,$info)) {
                location(- 1);
            } else {
                alert('修改失败！','back');
            }
        }
        
        // 修改操作
         if ($this->request->isPost()) {
            // 获取数据
            $gid = input('gid/s');
            $name = input('name/s');
            $link = input('link/s');
            $logo = input('logo/s');
            $sorting = input('sorting/d');
            
            if (! $gid) {
                $gid = $this->model->getMaxGid() + 1;
            }
            
            if (! $name) {
                alert('名称不能为空！','back');
            }
            
            if (! $link) {
                alert('链接不能为空！','back');
            }
            // 构建数据
            $data = array(
                'gid' => $gid,
                'name' => $name,
                'link' => $link,
                'logo' => $logo,
                'sorting' => $sorting,
                'update_user' => session('username')
            );
            
            // 执行添加
            if ($this->model->modLink($id, $data)) {
               $this->success('修改成功！', url('/Links/index'));
            } else {
                location(- 1);
            }
        } else {
            // 调取修改内容
            if (! $result = $this->model->getLink($id)) {
                $this->error('编辑的内容已经不存在！', - 1);
            }
            $this->assign('gids', $this->model->getGid());
            $this->assign('link', $result);
			return $this->fetch("mod");
        }
    }
}